﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
using System.Web.Security;
using System.IO;
namespace WebTeamplate
{

    public partial class Admin_ChangePassword : BasePage
    {
        #region Fields
        public string urlList = "User.aspx";
        #endregion

        #region ViewStates

        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {               
                InitTextControl();
                txtOldPass.Focus();
            }

        }
        #endregion

        #region Methos

        private void InitTextControl()
        {
            string validateGroup = "user";
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            refvOldPass.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblOldPass));
            refvOldPass.ValidationGroup = validateGroup;
            refvPassWord.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblPassword));
            refvPassWord.ValidationGroup = validateGroup;
            regvPassword.ValidationExpression = "\\w{" + (Membership.Provider.MinRequiredPasswordLength).ToString() + ",}";
            regvPassword.ValidationGroup = validateGroup;
            regvPassword.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_LENGTH, CommonFunction.GetTextLabel(lblPassword), Membership.Provider.MinRequiredPasswordLength - 1);
            refvConfirmPass.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblConfirmPassword));
            refvConfirmPass.ValidationGroup = validateGroup;
            cpvConfirmpass.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_COMPARE, CommonFunction.GetTextLabel(lblConfirmPassword), CommonFunction.GetTextLabel(lblPassword));
            cpvConfirmpass.ValidationGroup = validateGroup;           
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;  
        }      
     
        #endregion

        #region Events, Handler
     
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                  //check old pass
                MembershipUser user = Membership.GetUser(this.userloginId);
                if (user != null)
                {
                    if (user.GetPassword() == txtOldPass.Text.Trim())
                    {
                        //change pass
                        if (user.ChangePassword(txtOldPass.Text, txtPassword.Text.Trim()))
                            lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_SUCCESSFULL;
                        else
                            lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
                    }
                    else
                        lblErrorMessage.Text = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT,CommonFunction.GetTextLabel(lblOldPass));

                }
            }
            else
            {
                vsumCategory.ShowMessageBox = false;
                vsumCategory.ShowSummary = true;
            }
        }
        #endregion
    }
}
